
********************************************************************************************************************************
						
														***Marlene Mauk, Max Groemping***
			***Online Disinformation Predicts Inaccurate Beliefs about Election Fairness among Winners and Losers***
											***Replication File  ##3 - MEASUREMENT MODELS 2***
						
********************************************************************************************************************************


** importing factor score for dependent variable (used only for descriptives; otherwise modeled latently)
clear
import delimited v1 pef1 pef2 pef6 pef7 pef8 id pef_w pef_w_se using ".\mplus\pef_factors.dat", delimiter(space, collapse) varnames(nonames)
drop v1 pef_w_se
destring pef*, force replace
sum pef_w
gen pef01 = ((pef_w-r(min))/(r(max)-r(min)))
save pef_factors.dta, replace

** importing factor scores for independent variables from Mplus
* electoral integrity based on PEI election-level data
clear
import delimited v1 pei1 pei2 pei6 pei7 pei8 eleid pei v2 using ".\mplus\pei_factors.dat", delimiter(space, collapse) varnames(nonames) 
drop v1 v2
destring pei1 pei2 pei6 pei7 pei8 pei, force replace
sum pei
gen pei01 = ((pei-r(min))/(r(max)-r(min)))
drop if eleid==998 /*drop the two hypothetical countries created for providing the metric*/
sum pei*
save pei_factors.dta, replace

* online disinformation based on V-Dem dat
clear
import delimited v1 v2smgovdom v2smfordom v2smpardom id disinfo v2 using ".\mplus\disinfo_factors.dat", delimiter(space, collapse) varnames(nonames) 
drop v1 v2
destring v2smgovdom v2smfordom v2smpardom id disinfo, force replace
sum disinfo
gen disinfo01 = ((disinfo-r(min))/(r(max)-r(min)))
save disinfo_factors.dta, replace

* electoral integrity based on PEI expert-level data (aggregated to election level)
clear
import delimited v1 pei1 pei2 pei6 pei7 pei8 pei_w pei_b v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 eleid using ".\mplus\pei_exp_factors.dat", delimiter(space, collapse) varnames(nonames) 
drop v1 v2-v11
destring pei1 pei2 pei6 pei7 pei8 pei_w pei_b, force replace
save pei_exp_factors_exp.dta, replace /*this file will be used for validation of measure*/
sum pei_b 
gen pei_exp01 = ((pei_b-r(min))/(r(max)-r(min)))
collapse(mean) pei_exp01 pei_b, by(eleid)
save pei_exp_factors.dta, replace


use mergeddata.dta
merge 1:1 id using pef_factors, keepusing(pef_w pef01)
tab cowcode if _merge==1 /*that's respondents who have missing on all pef1 pef2 pef6 pef7 pef8*/
drop _merge
merge m:1 eleid using pei_factors.dta, keepusing(pei pei01)
drop _merge
merge 1:1 id using disinfo_factors.dta, keepusing(disinfo disinfo01)
drop _merge
merge m:1 eleid using pei_exp_factors.dta, keepusing(pei_exp01 pei_b)
drop _merge

stata2mplus id cowcode eleyear elecow gdpgrowth internet pei01 pef1 pef2 pef6 pef7 pef8 dommonrestr intmonrestr polint age edu_1 edu_2 edu_3 satdem income soctrust winner usesm01 powerusesm ///
disinfo01 v2xme_altinf v2eltrnout v2elaccept pei_exp01 nelda11 v2xel_frefair v2smgovdom v2smpardom v2smfordom v2x_regime democracy study wave using mergeddata, missing(999) replace
save mergeddata.dta, replace